package com.PGD.ui.views.common.components
{
	import flash.events.FocusEvent;
	import flash.events.MouseEvent;
	
	import mx.controls.RichTextEditor;
	import mx.events.FlexEvent;

	public class RichTextEditorWithUnderlinedLink extends RichTextEditor
	{
		public function RichTextEditorWithUnderlinedLink()
		{
			super();
			this.addEventListener(FlexEvent.CREATION_COMPLETE,cc);
			

			
			
		}
		
		private function cc(evt:FlexEvent):void
		{
			linkTextInput.addEventListener(FocusEvent.FOCUS_OUT, onLinkFocusOut );
		}
		private function onLinkFocusOut( e:FocusEvent ):void {
            // the RTE does some RegEx work itself to replace the things that I'm replacing here each time 
            // something is commited. That's why there's some funky redundancy going on.
            var html:String = this.htmlText;
            
            //TARGET="_blank"><U>
            var blankU:RegExp = /TARGET="_blank"><U>/g;
            html = html.replace( blankU, "><U>" );
            
            // remove the TARGET="_blank" in the links and replace it with an <U>         
            var removeBlank:RegExp = /TARGET="_blank">/g;
            html = html.replace( removeBlank, "><U>" );
            
            // close the underline
            var closeUnderline:RegExp = /<\/A>/g;
            html = html.replace( closeUnderline, "</U></A>" );
            
            // make the RTE think that the underline button was pressed so that it will 
            // apply the appropritate style to make the underline visible
            var me:MouseEvent = new MouseEvent( MouseEvent.CLICK, false );
            this.underlineButton.dispatchEvent( me );
            
            // now there are some double "</U></U>" so we need to replace them with </U>
            var uu:RegExp = /<\/U><\/U>/g;
            html = html.replace( uu, "</U>" );
            
           
        }

		
	}
}